<?php
    if(!defined('LT2N_ADMIN')){die('bạn không có quyền truy cập'); }
    $db = new Model();
    $module['name'] = $_GET["mod"];
    if ( $module['name'] == 'room' ) {
        if( isset( $_GET['act'] ) ) {

            $act = $_GET['act'];

            if ( $act == 'list') {

                $rooms = $db->select( LT2N_PREFIX . '_rooms', "", "ORDER BY `room_name` ASC" );


            } elseif ( $act == 'add' ) {
                initalize_post_var('room_name','room_desc', 'room_status');
                if(isset($_SESSION['permission']) && $_SESSION['permission'] != PERMISSION_USER && $_SESSION['permission'] != PERMISSION_MANAGER ){
                    if(isset($_POST['btnAdd'])){
                        $room_name   = $_POST['room_name'];

                        $db->select( LT2N_PREFIX.'_rooms', array( '`room_name`' => $room_name ) );
                        if ( $db->check( array( true => false ) ) ) {
                            $errors[] = '<div class="error" >Phòng '. $room_name .' đã tồn tại.</div>';
                        } else {

                            $room_desc   = $_POST['room_desc'];
                            $room_status = $_POST['room_status'];
                            $list = array(
                                'room_name' =>$room_name,
                                'room_desc' =>$room_desc,
                                'room_status' => $room_status
                            );


                            $db->insert(LT2N_PREFIX.'_rooms',$list);
                            if($db->checkQuery() == 1){
                                $success[] = '<div class="success" >Đã thêm thành công.</div>';
                                header("Location: index.php?mod=room&act=list&success=".SUCCESS_ADD);
                            } else {
                                $errors[] = '<div class="error" >Thêm phòng mới thất bại.</div>';
                            }
                        }

                    }

                }
            } elseif ( $act == 'edit' ) {
                    $arr = $db->select(LT2N_PREFIX.'_rooms',array('room_id'=>$_GET['id']));
                    if(isset($_SESSION['permission']) && $_SESSION['permission'] != PERMISSION_USER && $_SESSION['permission'] != PERMISSION_MANAGER ){
                        if(isset($_POST['btnUpdate'])){

                            $room_name   = $_POST['room_name'];
                            $room_desc   = $_POST['room_desc'];
                            $room_status = $_POST['room_status'];
                            $list = array(
                                'room_name' => $room_name,
                                'room_desc' => $room_desc,
                                'room_status' => $room_status
                            );


                            $key = "`room_id` = ". $_GET['id'];

                            $db->update(LT2N_PREFIX.'_rooms', $list, $key);
                            header("Location: index.php?mod=room&act=list&success=".SUCCESS_EDIT);

                        }


                    }
            } elseif ( $act == 'status' ) {
                if(isset($_SESSION['permission']) && $_SESSION['permission'] != PERMISSION_USER && $_SESSION['permission'] != PERMISSION_MANAGER ){
                    $now = $_GET['now'];
                    $id = $_GET['id'];
                    if ( $now == 1 ) {
                        $list = array(
                            'room_status' => 0
                        );
                    } elseif ( $now == 0 ) {
                        $list = array(
                            'room_status' => 1
                        );
                    }
                    $key = "`room_id` = ". $_GET['id'];
                    $db->update(LT2N_PREFIX.'_rooms', $list, $key);
                    header("Location: index.php?mod=room&act=list");
                }
            } elseif ( $act == 'del' ) {
                if(isset($_SESSION['permission']) && $_SESSION['permission'] != PERMISSION_USER && $_SESSION['permission'] != PERMISSION_MANAGER ){
                    $db->delete(LT2N_PREFIX.'_rooms',array('room_id'=>$_GET['id']));
                    if ( $db->check(array( true => false )) ) {
                        header("location: index.php?mod=room&act=list");
                    } else {
                        header("location: index.php?mod=room&act=edit&id=" . $_GET['id'] . "");
                    }
                }
            } elseif ( $act == 'search' ) {
                // tim kiem phong
                $rooms = array();
                $s='';
                if ( isset( $_POST['btnSearchRoom'] ) ) {
                    $s = ( isset( $_POST['s'] ) ) ? $_POST['s'] : '';
                    $rooms = $db->select( LT2N_PREFIX.'_rooms', "`room_name` LIKE '%". $s ."%'" );
                }
            } elseif ( $act == 'statistics' ) {
                $rooms_by_status = $db->select( LT2N_PREFIX.'_rooms', '', "GROUP BY `room_status`", " COUNT(*) as num_room, `room_status` " );
            } elseif ( $act == '' ) {
                header("location: index.php?mod=" . $module['name'] . "&act=list");
            }
            require(LT2N_VIEW_PATH.$module["name"]."/".$act.".php");
        }
    }
?>